// miniprogram/pages/index-admin/index-admin.js
import {
  $wuxToast
} from '../../miniprogram_npm/wux-weapp/index'

var bmap = require('../../libs/bmap-wx.js');
var wxMarkerData = [];
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: [],
    longitude: '',
    latitude: '',
    address: '正在定位...'
  },

  getLocationView: function (e) {
    this.goPage('location');
  },

  food: function (e) {
    this.goPage('food');
  },

  restaurant: function (e) {
    var that = this
    if (that.data.addrlatitudeess != '' && that.data.longitude != '') {
      this.goPage('restaurant');
    } else {
      $wuxToast().show({
        type: 'text',
        duration: 1500,
        color: '#fff',
        text: '没有获取到位置信息'
      })
    }
  },

  book: function (e) {
    this.goPage('book');
  },

  week: function (e) {
    this.goPage('week');
  },

  shop: function (e) {
    $wuxToast().show({
      type: 'text',
      duration: 1500,
      color: '#fff',
      text: '正在开发中...'
    })
    // this.goPage('shop');
  },

  travel: function (e) {
    $wuxToast().show({
      type: 'text',
      duration: 1500,
      color: '#fff',
      text: '正在开发中...'
    })
    // this.goPage('travel');
  },

  //页面跳转
  goPage: function (pageName) {
    //页面跳转
    wx.navigateTo({
      url: '../' + pageName + '/' + pageName
    })
  },

  getUserLocation: function () {
    var that = this
    var ak = app.globalData.baiduAK
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: ak
    });
    var fail = function (data) {
      // console.log(data)
      that.setData({
        address: '点击获取位置信息'
      });
    };
    var success = function (data) {
      // console.log(data)
      wxMarkerData = data.wxMarkerData;

      app.globalData.city = data.originalData.result.addressComponent.city
      app.globalData.locationName = data.originalData.result.pois[0].name
      app.globalData.address = wxMarkerData[0].address
      app.globalData.longitude = wxMarkerData[0].longitude
      app.globalData.latitude = wxMarkerData[0].latitude

      that.setData({
        markers: wxMarkerData,
        latitude: wxMarkerData[0].latitude,
        longitude: wxMarkerData[0].longitude,
        address: app.globalData.locationName
      });

      // console.log(app.globalData)
    }
    // 发起regeocoding检索请求 
    BMap.regeocoding({
      fail: fail,
      success: success,
      iconPath: '../../images/map/marker_red.png',
      iconTapPath: '../../images/map/marker_red.png'
    });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getUserLocation()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    // console.log(app.globalData)
    if (app.globalData.longitude != '' && app.globalData.latitude != '') {
      that.setData({
        longitude: app.globalData.longitude,
        latitude: app.globalData.latitude,
        address: app.globalData.locationName
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})